package org.example.aienglishapp_login.controller.operationlog;

import com.alibaba.excel.EasyExcel;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.example.aienglishapp_login.annotation.debounce.Debounce;
import org.example.aienglishapp_login.entity.operationlog.OperationLog;
import org.example.aienglishapp_login.service.operationlog.OperationLogService;
import org.example.aienglishapp_login.util.response.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * @author Asus
 */
@Slf4j
@RestController
@RequestMapping("/api/operationlogs")
public class OperationLogController {

    @Autowired
    private OperationLogService operationLogService;

    /**
     * Create a new operation log.
     *
     * @param log The operation log to create.
     */
    @PostMapping
    @Debounce(interval = 500)
    public void createOperationLog(@RequestBody OperationLog log) {
        operationLogService.insertOperationLog(log);
    }

    /**
     * Get all operation logs.
     *
     * @return A list of all operation logs.
     */
    @GetMapping
    public ResponseResult<List<OperationLog>> getAllOperationLogs() {
        List<OperationLog> logs = operationLogService.getAllOperationLogs();
        log.info("Returning {} operation logs", logs.size());
        return ResponseResult.success(logs);
    }

    /**
     * Delete an operation log by ID.
     *
     * @param id The ID of the operation log to delete`.
     * @return A response entity with no content.
     */
    @DeleteMapping("/{id}")
    @Debounce(interval = 500)
    public ResponseEntity<Void> deleteById(@PathVariable Long id) {
        log.info("Deleting operation log with ID: {}", id);
        operationLogService.deleteById(id);
        return ResponseEntity.noContent().build();
    }

    /**
     * 导出
     *
     * @param response     响应对象
     * @param operationLog 查询对象
     */
    @GetMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response, OperationLog operationLog) throws IOException {
        // 设置响应头
        response.setContentType("application/vnd.openxmlformatsofficedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");

        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("操作日志", StandardCharsets.UTF_8).replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");

        // 查询列表数据
        List<OperationLog> list = operationLogService.getAllOperationLogs();

        // 导出数据
        EasyExcel.write(response.getOutputStream(), OperationLog.class).sheet("操作日志").doWrite(list);
    }
}
